package com.demo.gateway.util;

import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 * @author: demo
 * @date: 2020年6月18日
 * @Document : 日期工具类
 */
public class DateUtil {
	private static final String[] DATE_FORMAT_PARSEPATTERNS = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
			"yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd",
			"yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM" };

	private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,sss";

	public static String getFormatDate(Date date, String format) {
		return DateFormatUtils.format(date, format);
	}

	public static String getFormatDate(Date date) {
		return DateFormatUtils.format(date, DEFAULT_DATE_FORMAT);
	}

	public static String getNow() {
		return DateFormatUtils.format(new Date(), DEFAULT_DATE_FORMAT);
	}

	public static Date getDate(String str) {
		Date date = null;
		try {
			date = DateUtils.parseDate(str, DATE_FORMAT_PARSEPATTERNS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
}
